@file:OptIn(ExperimentalMaterial3Api::class)

package com.shenzhenai.composesideeffects.samples

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.shenzhenai.composesideeffects.ui.theme.DN_Compose_LearnTheme
import kotlinx.coroutines.launch

/**
 * @author huangzehui
 * @date 2025/9/19 13:12 周五
 * @description
 *
 *
 * rememberCoroutineScope
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.videopod.episodes&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=80
 */


@Composable
fun RememberCoroutineScopeSample() {
    val scaffoldState = rememberBottomSheetScaffoldState()

    // 抽屉的状态
    val drawerState = rememberDrawerState(DrawerValue.Closed)

    val coroutineScope = rememberCoroutineScope()

    // 抽屉的组合函数
    ModalNavigationDrawer(
        drawerState = drawerState,
        drawerContent = { Text(text = "Hello") },
        gesturesEnabled = true,
    ) {
        Scaffold(
            // 标题栏区域
            topBar = {
                TopAppBar(
                    title = { Text(text = "脚手架示例") },
                    navigationIcon = {
                        IconButton(onClick = {
                            coroutineScope.launch {
                                if (drawerState.isOpen){
                                    drawerState.close()
                                } else {
                                    drawerState.open()
                                }
                            }
                        }) {
                            Icon(
                                imageVector = Icons.Filled.Menu,
                                contentDescription = null
                            )
                        }
                    }
                )
            },

            // 屏幕内容区域
            content = { paddingValues ->
                Box(
                    modifier = Modifier
                        .padding(paddingValues)
                        .fillMaxSize(),
                    contentAlignment = Alignment.Center,
                ) {
                    Text(text = "屏幕内容区域")
                }



            },

            floatingActionButton = {
                ExtendedFloatingActionButton(onClick = {
                    coroutineScope.launch {
                        scaffoldState.snackbarHostState.showSnackbar(
                            message = "点击了悬浮按钮",
                        )
                    }
                }, content = {
                    Text(text = "悬浮按钮")
                })
            },

            snackbarHost = {
                // Snackbar 展示
                SnackbarHost(scaffoldState.snackbarHostState)
            }
        )
    }



}

@Preview
@Composable
fun RememberCoroutineScopeSamplePreview() {
    DN_Compose_LearnTheme {
        RememberCoroutineScopeSample()
    }
}